package com.sl.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.Map;

public class ExdeUtil {
	/**
	 * 根据传入时间来获取本周的时间范围
	 * @param a 传进来的时间String类型
	 * @return
	 */
	public static Map thisWeekDate(String a){
		SimpleDateFormat sdf  = new SimpleDateFormat("yyyy-MM-dd");
		Date nowDate = null;
		try {
			nowDate = sdf.parse(a);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}  
		Calendar cal = Calendar.getInstance();  
	     cal.setTime(nowDate);  
	     // 判断要计算的日期是否是周日，如果是则减一天计算周六的，否则会出问题，计算到下一周去了  
	     int dayWeek = cal.get(Calendar.DAY_OF_WEEK);// 获得当前日期是一个星期的第几天  
	     if (1 == dayWeek) {  
	        cal.add(Calendar.DAY_OF_MONTH, -1);  
	     }  
	     // System.out.println("要计算日期为:" + sdf.format(cal.getTime())); // 输出要计算日期  
	     // 设置一个星期的第一天，按中国的习惯一个星期的第一天是星期一  
	     cal.setFirstDayOfWeek(Calendar.MONDAY);  
	     // 获得当前日期是一个星期的第几天  
	     int day = cal.get(Calendar.DAY_OF_WEEK);  
	     // 根据日历的规则，给当前日期减去星期几与一个星期第一天的差值  
	     cal.add(Calendar.DATE, cal.getFirstDayOfWeek() - day);  
	     String imptimeBegin = sdf.format(cal.getTime());  
	     // System.out.println("所在周星期一的日期：" + imptimeBegin);  
	     cal.add(Calendar.DATE, 6);  
	     String imptimeEnd = sdf.format(cal.getTime());  
	     // System.out.println("所在周星期日的日期：" + imptimeEnd);  
	     //return imptimeBegin + "," + imptimeEnd;
	     Map<String,String> condition=new HashMap<String,String>();
	     condition.put("beginDate", imptimeBegin);
	     condition.put("endDate", imptimeEnd);
		return condition;
	}
	
	
	public static String datechange(Date date, String pattern) {  
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);  
        String demo = sdf.format(date);  
        return demo;  
    }
	
	/**
	 * 根据“-”来拆分字符串
	 * @param str
	 * @return
	 */
	public static String splitString (String str){
		String[] s = str.split("-");
		str = "";
		for(int i=0;i<s.length;i++){
			str += s[i];
		}
		return str;
	}
	
	public static String findYearMonth()
	{
		/**
		 * 声明一个int变量year
		 */
		int year;
		/**
		 * 声明一个int变量month
		 */
		int month;
		/**
		 * 声明一个字符串变量date
		 */
		String date;
		/**
		 * 实例化一个对象calendar
		 */
		Calendar calendar = Calendar.getInstance();
		/**
		 * 获取年份
		 */
		year = calendar.get(Calendar.YEAR);
		/**
		 * 获取月份
		 */
		month = calendar.get(Calendar.MONTH) + 1;
		/**
		 * 拼接年份和月份
		 */
		date = year +""+ ( month<10 ? "0" + month : month);
		/**
		 * 返回当前年月
		 */
		return date;
	}
	
	
	/**
	 * 获取本周开始时间
	 * @return
	 */
    public static Date getThisWeekBegin() {
        Calendar cal = Calendar.getInstance();
        cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONDAY), cal.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
        cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
        return cal.getTime();
    }  
  
    /**
     * 获取本周结束时间
     * @return
     */
    public static Date getThisWeekEnd() {
        Calendar cal = Calendar.getInstance();
        cal.setTime(getThisWeekBegin());
        cal.add(Calendar.DAY_OF_WEEK, 7);
        return cal.getTime();
    }
  
    /**
     * 获取本月开始时间
     * @return
     */
    public static Date getThisMonthBegin() {
        Calendar cal = Calendar.getInstance();
        cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONDAY), cal.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
        cal.set(Calendar.DAY_OF_MONTH, cal.getActualMinimum(Calendar.DAY_OF_MONTH));
        return cal.getTime();
    }
  
    /**
     * 获取本月结束时间
     * @return
     */
    public static Date getThisMonthEnd() {
        Calendar cal = Calendar.getInstance();
        cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONDAY), cal.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
        cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH));
        cal.set(Calendar.HOUR_OF_DAY, 24);
        return cal.getTime();
    }
    
    /**
     * 获取本季度开始时间
     * @return
     */
    public static Date getThisQuarterBegin() {
        Calendar c = Calendar.getInstance();
        int currentMonth = c.get(Calendar.MONTH) + 1;
        SimpleDateFormat longSdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        SimpleDateFormat shortSdf = new SimpleDateFormat("yyyy-MM-dd");
        Date now = null;
        try {
            if (currentMonth >= 1 && currentMonth <= 3)
                c.set(Calendar.MONTH, 0);
            else if (currentMonth >= 4 && currentMonth <= 6)
                c.set(Calendar.MONTH, 3);
            else if (currentMonth >= 7 && currentMonth <= 9)
                c.set(Calendar.MONTH, 4);
            else if (currentMonth >= 10 && currentMonth <= 12)
                c.set(Calendar.MONTH, 9);
            c.set(Calendar.DATE, 1);
            now = longSdf.parse(shortSdf.format(c.getTime()) + " 00:00:00");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return now;
    }
    
    /**
     * 获取本季度结束时间
     * @return
     */
    public static Date getThisQuarterEnd() {
		Calendar cal = Calendar.getInstance();
		cal.setTime(getThisQuarterBegin());
		cal.add(Calendar.MONTH, 3);
		return cal.getTime();
    }
    
    /**
     * 获取本年开始时间
     * @return
     */
    public static Date getThisYearBegin() {
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.YEAR, getNowYear());
        cal.set(Calendar.MONTH, Calendar.JANUARY);
        cal.set(Calendar.DATE, 1);
        return cal.getTime();
    }

    /**
     * 获取本年结束时间
     * @return
     */
    public static Date getThisYearEnd() {
        Calendar cal = Calendar.getInstance();
        cal.setTime(getThisYearBegin());
        cal.add(Calendar.YEAR, 1);
        return cal.getTime();
    }
	/**
	 * 获取今年是哪一年
	 * @return
	 */
	public static Integer getNowYear() {
		Date date = new Date();
		GregorianCalendar gc = (GregorianCalendar) Calendar.getInstance();
		gc.setTime(date);
		return Integer.valueOf(gc.get(1));
	}
	
	/**
	 * 获取之前开始时间
	 * @param type
	 * @param value
	 * @return
	 */
	public static Date getTodayBefore(String type, String value) {
		int beforeNum = 0;
		beforeNum = str2int(value) * -1;
		// 得到一个Calendar的实例
		Calendar ca = Calendar.getInstance();
		// 设置时间为当前时间
		ca.setTime(new Date());
		// 格式 HH:MM:SS
		ca.set(Calendar.HOUR_OF_DAY, 0);  
		ca.set(Calendar.MINUTE, 0);  
		ca.set(Calendar.SECOND, 0);
		if(type.toUpperCase().equals("DAY")) {
			ca.add(Calendar.DATE, beforeNum);
		}else if(type.toUpperCase().equals("WEEK")) {
			ca.add(Calendar.DATE, beforeNum * 7);
		}else if(type.toUpperCase().equals("MONTH")) {
			ca.add(Calendar.MONTH, beforeNum);
		}else if(type.toUpperCase().equals("YEAR")) {
			ca.add(Calendar.YEAR, beforeNum);
		}
		Date resultDate = ca.getTime(); // 结果  
		return resultDate;
	}
	
	public static Date getTodayEnd() {
		Calendar ca = Calendar.getInstance();
		ca.setTime(new Date());
		ca.set(Calendar.HOUR_OF_DAY, 23);  
		ca.set(Calendar.MINUTE, 59);  
		ca.set(Calendar.SECOND, 59);
		Date resultDate = ca.getTime(); // 结果  
		return resultDate;
	}
	
	public static Date getTodayBegin() {
		// 得到一个Calendar的实例
		Calendar ca = Calendar.getInstance();
		// 设置时间为当前时间
		ca.setTime(new Date());
		// 格式 HH:MM:SS
		ca.set(Calendar.HOUR_OF_DAY, 0);  
		ca.set(Calendar.MINUTE, 0);  
		ca.set(Calendar.SECOND, 0);
		Date resultDate = ca.getTime(); // 结果  
		return resultDate;
	}

	/**
	 * 获取之前开始时间
	 * @param type
	 * @param value
	 * @return
	 */
	public static Date getDateBefore(Date date,String type, String value) {
		int beforeNum = 0;
		beforeNum = str2int(value) * -1;
		// 得到一个Calendar的实例
		Calendar ca = Calendar.getInstance();
		// 设置时间为当前时间
		ca.setTime(date);
		// 格式 HH:MM:SS
		ca.set(Calendar.HOUR_OF_DAY, 0);  
		ca.set(Calendar.MINUTE, 0);  
		ca.set(Calendar.SECOND, 0);
		if(type.toUpperCase().equals("DAY")) {
			ca.add(Calendar.DATE, beforeNum);
		}else if(type.toUpperCase().equals("WEEK")) {
			ca.add(Calendar.DATE, beforeNum * 7);
		}else if(type.toUpperCase().equals("MONTH")) {
			ca.add(Calendar.MONTH, beforeNum);
		}else if(type.toUpperCase().equals("YEAR")) {
			ca.add(Calendar.YEAR, beforeNum);
		}
		Date resultDate = ca.getTime(); // 结果  
		return resultDate;
	}
	
	public static Date getDateEnd(Date date) {
		Calendar ca = Calendar.getInstance();
		ca.setTime(date);
		ca.set(Calendar.HOUR_OF_DAY, 23);  
		ca.set(Calendar.MINUTE, 59);  
		ca.set(Calendar.SECOND, 59);
		Date resultDate = ca.getTime(); // 结果  
		return resultDate;
	}
	
	public static Date getDateBegin(Date date) {
		// 得到一个Calendar的实例
		Calendar ca = Calendar.getInstance();
		// 设置时间为当前时间
		ca.setTime(date);
		// 格式 HH:MM:SS
		ca.set(Calendar.HOUR_OF_DAY, 0);  
		ca.set(Calendar.MINUTE, 0);  
		ca.set(Calendar.SECOND, 0);
		Date resultDate = ca.getTime(); // 结果  
		return resultDate;
	}
	
	/**
	 * 获取周期时间偏移量
	 * @param type
	 * @param value
	 * @return
	 */
	public static int getCycDateOffset(String type, String value) {
		Calendar base = Calendar.getInstance();
		Calendar now = Calendar.getInstance();
		base.set(2018, 0, 1, 0, 0, 0);//默认已2018-01-01开始计算
		now.setTime(new Date());
		int result = 0;
		int valueNum = str2int(value);
		if(valueNum == 0) {//默认周期值为1
    			valueNum = 1;
		}
		if(type.toUpperCase().equals("DAY")) {
			int dayOffset = (int) Math.floor(( now.getTimeInMillis() - base.getTimeInMillis() ) /(1000*3600*24));
			result = (int) Math.floor(dayOffset / valueNum);
		}else if(type.toUpperCase().equals("WEEK")) {
			int baseOffset = base.get(Calendar.DAY_OF_WEEK) - 1;
			int dayOffset = (int) Math.floor(( now.getTimeInMillis() - base.getTimeInMillis() ) /(1000*3600*24)) + baseOffset;
			int weekOffset = (int) Math.floor(dayOffset / 7);
			result = (int) Math.floor(weekOffset / valueNum);
			
		}else if(type.toUpperCase().equals("MONTH")) {
			int month = now.get(Calendar.MONTH) - base.get(Calendar.MONTH);
	        int year = (now.get(Calendar.YEAR) - base.get(Calendar.YEAR))*12;
	        int monthOffset = month + year;
	        
	        result = (int) Math.floor(monthOffset / valueNum);
		}else if(type.toUpperCase().equals("YEAR")) {
			int yearOffset = (now.get(Calendar.YEAR) - base.get(Calendar.YEAR));
			result = (int) Math.floor(yearOffset / valueNum);
		}
		return result;
	}
	
	/**
	 * 获取周期偏移开始时间
	 * @param type
	 * @param value
	 * @param offset
	 * @return
	 */
	public static Date getCycDate(String type, String value, int offset) {
		// 得到一个Calendar的实例
		Calendar base = Calendar.getInstance();
		// 设置时间为当前时间
		base.set(2018, 0, 1, 0, 0, 0);//默认已2018-01-01开始计算
		
		int valueNum = str2int(value);
		if(valueNum == 0) {//默认周期值为1
    			valueNum = 1;
		}
		
		if(type.toUpperCase().equals("DAY")) {
			base.add(Calendar.DATE, valueNum * offset);
		}else if(type.toUpperCase().equals("WEEK")) {
			int baseOffset = base.get(Calendar.DAY_OF_WEEK) - 1;
			base.add(Calendar.DATE, valueNum * offset * 7 - baseOffset);
		}else if(type.toUpperCase().equals("MONTH")) {
			base.add(Calendar.MONTH, valueNum * offset);
		}else if(type.toUpperCase().equals("YEAR")) {
			base.add(Calendar.YEAR, valueNum * offset);
		}
		Date resultDate = base.getTime(); // 结果  
		return resultDate;
	}

	public static int str2int(String str) {
		int value = 0;
		if(str != null && !"".equals(str.replaceAll(" ", ""))) {
			try {
				int temp = Integer.valueOf(str.replaceAll(" ", ""));
				value = temp;
			} catch (Exception e) {}
		}
		return value;
	}

	public static double str2double(String str) {
		double value = 0;
		if(str != null && !"".equals(str.replaceAll(" ", ""))) {
			try {
				double temp = Double.valueOf(str.replaceAll(" ", ""));
				value = temp;
			} catch (Exception e) {}
		}
		return value;
	}
	
	public static float str2float(String str) {
		float value = 0;
		try {
			str = str.replaceAll(" ", "");
			if(str != null && !"".equals(str)) {
				float temp = Float.valueOf(str);
				value = temp;
			}
		} catch (Exception e) {}
		return value;
	}
	
	public static Date str2Date(String source,String pattern) {
		Date value = null;
		try {
			SimpleDateFormat sdf = new SimpleDateFormat(pattern);
			value = sdf.parse(source);
		} catch (ParseException e) {
			value = null;
		}
		return value;
	}
	
	public static boolean str2boolean(Object obj) {
		boolean value = false;
		//toLowerCase()
		try {
			String str = String.valueOf(obj);
			str = str.replaceAll(" ", "");
			if(str != null && !"".equals(str)) {
				if(str.toLowerCase().equals("true")) {
					value = true;
				}
			}
		} catch (Exception e) {}
		return value;
	};
}
